package de.uni.muenster.idw2.threads;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;

public class ImageLoaderThread extends AsyncTask<Void, Void, Drawable>{

	private final String path;
	private final Activity act;
	private ProgressDialog progressDialog;

	public ImageLoaderThread(String path, Activity act){
		this.path = path;
		this.act = act;
		
		progressDialog = new ProgressDialog((Context) act);
		progressDialog.setMessage("Loading...");
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog.show();
	}
	
	@Override
	protected Drawable doInBackground(Void... params) {
		
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			Log.e("wichtig", e.toString());
		}
		
		Drawable img = null;
		try {
			 img = Drawable.createFromStream((InputStream) new URL(path).getContent(), "draw");
		} catch (MalformedURLException e) {
			Log.e("wichtig", e.toString());
		} catch (IOException e) {
			Log.e("wichtig", e.toString());
		}
		return img;
	}
	
	@Override
	protected void onPostExecute(Drawable result) {
		super.onPostExecute(result);
		progressDialog.dismiss();
		
		Builder builder = new Builder(act);
		builder.setIcon(result);
		builder.setTitle("hallo");
		builder.show();
	}
	
	@Override
	protected void onCancelled() {
		super.onCancelled();
		progressDialog.dismiss();
	}

}
